﻿namespace IBTrader.Charts.Legend
{
    using IBTrader.Modules.Read;
    using System.Drawing;
    using System.Linq;

    class LegendPrice : BaseChart
    {
        public LegendPrice(Worker worker) : base(worker) { }
        public override void Draw(IGraphics g, Charts.Region region)
        {
            if (!Lines.Any()) return;
            var interval = region.Y.Interval(Lines.ToArray().Select(l => l.Price));
            var lines = Lines.GroupBy(l => (int)(l.Price / interval)).Select(r => r.OrderBy(l => l.Price).First());
            foreach (Line line in lines.ToArray())
            {
                var y = region.y(line);
                g.DrawString(String(line.Price, interval), Font, new SolidBrush(Color), new PointF(0, y));
                g.DrawLines(new Pen(Color), new PointF(0, y), new PointF((float)Indicator.Base.NaN(region.X.Max / region.X.Pixel), y));
            }
        }
        protected virtual string String(double price, double _)
        {
            return price.ToString();
        }
    }
}